﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;

namespace 显示箱子名字
{
    public class NamedChestDisplay : ModSystem
    {
        public override void PostDrawTiles()
        {
            var config = ModContent.GetInstance<YourModConfig>();
            Color textColor = config.TextColor;
            float baseScale = 0.35f;
            float zoomScale = Main.GameViewMatrix.Zoom.X;
            float adjustedScale = baseScale / zoomScale;
            DynamicSpriteFont font = FontAssets.DeathText.Value;

            Main.spriteBatch.Begin(
                SpriteSortMode.Deferred,
                BlendState.AlphaBlend,
                SamplerState.LinearClamp,
                DepthStencilState.None,
                RasterizerState.CullCounterClockwise,
                null,
                Main.GameViewMatrix.TransformationMatrix
            );

            for (int i = 0; i < Main.maxChests; i++)
            {
                Chest chest = Main.chest[i];
                if (chest == null || string.IsNullOrEmpty(chest.name))
                    continue;
                Vector2 chestWorldPos = new(chest.x * 16 + 16, chest.y * 16 + 16);
                Vector2 screenPos = chestWorldPos - Main.screenPosition;
                Vector2 textSize = font.MeasureString(chest.name) * adjustedScale;
                Vector2 drawPos = screenPos - new Vector2(textSize.X * 0.5f, textSize.Y + 4);
                Main.spriteBatch.DrawString(
                    font,
                    chest.name,
                    drawPos,
                    textColor,
                    0f,
                    Vector2.Zero,
                    adjustedScale,
                    SpriteEffects.None,
                    0f
                );
            }

            Main.spriteBatch.End();
        }
    }
}